//
//  MySearchDetailViewController.m
//  TTMusic
//
//  Created by yanghuiyan on 16/4/4.
//  Copyright © 2016年 cpy. All rights reserved.
//

#import "MySearchDetailViewController.h"
#import "MySearchSingleSongCollectionViewCell.h"
#import "CPYSONGSModel.h"
#import "CPYDiscoveryPageSongListModel.h"
#import "SearchSongDataModel.h"

@interface MySearchDetailViewController ()<UIScrollViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UISegmentedControl *segment;

@property (nonatomic ,strong) UICollectionViewFlowLayout *flowLayout;

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, strong) SearchSongDataModel *datamodel;

@property (nonatomic, strong) CPYSONGSModel *songsModel;

@end

@implementation MySearchDetailViewController


- (void)viewWillAppear:(BOOL)animated {
    [self dataRequest];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.dataSource = [NSMutableArray array];
    [self creatView];
    [self dataRequest];
    
    
}

#pragma mark - creatView
- (void)creatView {
    self.view.backgroundColor = [UIColor whiteColor];
    
    //自定义标题视图
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    titleLabel.text = @"搜索详情";
    titleLabel.font = [UIFont boldSystemFontOfSize:25];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    
    //导航栏左侧按钮
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"返回"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ] style:UIBarButtonItemStylePlain target:self action:@selector(leftItemAction:)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    // 创建collectionView
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.itemSize =CGSizeMake(SysWidth, SysHeight - 114);
    self.flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.flowLayout.minimumLineSpacing = 0;
    self.flowLayout.minimumInteritemSpacing = 0;
    _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SysWidth, SysHeight - 114) collectionViewLayout:_flowLayout];
    _collectionView.backgroundColor = [UIColor redColor];
    
    // 单曲
    [self.collectionView registerClass:[MySearchSingleSongCollectionViewCell class] forCellWithReuseIdentifier:@"SingleSong"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.view addSubview:self.collectionView];
  }

#pragma mark - 左侧按钮点击事件
- (void) leftItemAction:(UIBarButtonItem *)button {
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


#pragma mark - collectionView协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
 
        MySearchSingleSongCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SingleSong" forIndexPath:indexPath];
    cell.songsModel = _songsModel;
                cell.backgroundColor = [UIColor whiteColor];
                if (_dataSource.count != 0) {

                    cell.dataSource = _dataSource;

                }
                return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
   
}

#pragma mark - 数据解析
- (void) dataRequest {
    
    // 单曲
    NSString *string = [NSString stringWithFormat:@"http://search.dongting.com/song/search?q=%@&page=1", self.str];
    [TTMusicAFNetworkTool GETNetWithUrl:string body:nil headerFile:nil response:TTMusicJSON success:^(id result) {
        
        for (NSDictionary *dic in result[@"data"]) {
            self.datamodel = [[SearchSongDataModel alloc] init];
            [self.datamodel setValuesForKeysWithDictionary:dic];
            [self.dataSource addObject:self.datamodel];
            
        }
        self.songsModel = [[CPYSONGSModel alloc] initWithDataSource:result];


        [self.collectionView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
